package com.japanesee_learning.sync;

import com.japanesee_learning.inter.CheckLoginInterface;
import com.japanesee_learning.until.ConfigServerUntil;
import com.japanesee_learning.until.HttpUtil;
import com.japanesee_learning.until.JsonUntil;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class CheckLoginSync extends AsyncTask<String, Integer, Integer> {
	private Context mContext;
	private String mUser;
	private String mPas;
	private int mResult;
	private CheckLoginInterface mLongin;
	private String TAG = CheckLoginSync.class.getSimpleName();
	private String httoRespStr;

	public CheckLoginSync(Context context, String strUser, String strPass,
			CheckLoginInterface login) {
		mContext = context;
		mPas = strPass;
		mUser = strUser;
		mLongin = login;
	}

	@Override
	protected void onPreExecute() {
	}

	@Override
	protected void onPostExecute(Integer result) {
		mLongin.checkLogin(mResult);
	}

	public String checkLogin() {
		String result = "0";
		try {
			String strParamId = "user=" + mUser;
			String strParamUser = "pass=" + mPas;
			String url = ConfigServerUntil.urlCheckLoginSevice + "?"
					+ strParamId + "&" + strParamUser;
			Log.v(TAG, "url=" + url);

			try {
				Log.v(TAG, "checkLogin() | URL service: "
						+ ConfigServerUntil.urlCheckLoginSevice);
				httoRespStr = HttpUtil.httpGetString(url,
						ConfigServerUntil.timeOut, 1);
				Log.v(TAG, "checkLogin() | Chuoi tra ve: " + httoRespStr);
				result = JsonUntil.parseResultUpdate(httoRespStr);
				Log.v(TAG, "checkLogin() | Ket qua thuc thi: " + result);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception ex) {
			Log.v(TAG, ex.getMessage());
		}
		return result;
	}

	@Override
	protected Integer doInBackground(String... params) {
		mResult = Integer.parseInt(checkLogin());
		return mResult;
	}
}